"""
房间 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz.family_biz import room_biz
from components.audit_log import audit
from components.depends import login_required
from components.global_param import get_user_id
from components.logger import log
from components.response import (
    OKResponse,
    OKWithDataResponse,
)
from models import OK
from models.request import (
    FamilyRoomCreateRequest,
    FamilyRoomDetailRequest,
    FamilyRoomListRequest,
    FamilyRoomPathField,
    FamilyRoomUpdateRequest,
)
from models.response import (
    FamilyRoomDetailResponse,
    FamilyRoomListResponse,
)
from models.vo import (
    FamilyRoomDetailVO,
    FamilyRoomListPage,
)
from utils.data_utils.special import ObjectIdStr

room_router: APIRouter = APIRouter(prefix="/room")
""" 房间 路由 """


@room_router.post(
    path="/",
    summary="房间 | 创建",
    response_model=FamilyRoomDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_ROOM,
    operation=enum.ModelOperation.UPLOAD,
    po_id="response.data.room_id",
)
async def room_create(request: FamilyRoomCreateRequest) -> OKWithDataResponse:
    vo: FamilyRoomDetailVO = room_biz.FamilyRoomCreator.create(request)
    log.info(f"[房间][创建] user_id: {get_user_id()} room_id: {vo.room_id}")

    return OKWithDataResponse(vo)


@room_router.delete(
    path="/{room_id}/",
    summary="房间 | 删除",
    response_model=OK,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_ROOM,
    operation=enum.ModelOperation.DELETE,
    po_id="room_id",
)
async def room_delete(room_id: ObjectIdStr = FamilyRoomPathField.room_id) -> OKResponse:
    request = FamilyRoomDetailRequest(room_id=room_id)
    room_biz.FamilyRoomDeleter.delete(request)
    log.info(f"[房间][删除] user_id: {get_user_id()} room_id: {room_id}")

    return OKResponse()


@room_router.put(
    path="/{room_id}/",
    summary="房间 | 更新",
    response_model=FamilyRoomDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_ROOM,
    operation=enum.ModelOperation.UPDATE,
    po_id="room_id",
)
async def room_update(
    request: FamilyRoomUpdateRequest,
    room_id: ObjectIdStr = FamilyRoomPathField.room_id,
) -> OKWithDataResponse:
    request.room_id = room_id
    vo: FamilyRoomDetailVO = room_biz.FamilyRoomUpdator.update(request)
    log.info(f"[房间][更新] user_id: {get_user_id()} room_id: {room_id}")

    return OKWithDataResponse(vo)


@room_router.get(
    path="/{room_id}/",
    summary="房间 | 详情",
    response_model=FamilyRoomDetailResponse,
)
@audit(
    model=enum.Model.FAMILY_ROOM,
    operation=enum.ModelOperation.DETAIL,
    po_id="room_id",
)
async def room_detail(room_id: ObjectIdStr = FamilyRoomPathField.room_id) -> OKWithDataResponse:
    request = FamilyRoomDetailRequest(room_id=room_id)
    vo: FamilyRoomDetailVO = room_biz.FamilyRoomGetter.detail(request)
    log.info(f"[房间][详情] user_id: {get_user_id()} room_id: {room_id}")

    return OKWithDataResponse(vo)


@room_router.get(
    path="/",
    summary="房间 | 列表",
    response_model=FamilyRoomListResponse,
)
@audit(
    model=enum.Model.FAMILY_ROOM,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.room_id",
)
async def room_list(request: FamilyRoomListRequest = Depends(FamilyRoomListRequest.from_query)) -> OKWithDataResponse:
    vo: FamilyRoomListPage = room_biz.FamilyRoomGetter.list(request)
    log.info(f"[房间][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)
